<?php
class db_model extends CI_Model {

	public function __construct()
	{
		$config['hostname'] = "localhost";
		$config['username'] = "jeff";
		$config['password'] = "jeff0909";
		$config['database'] = "career";
/*
		if(LANGUAGE=='zh_TW')
			$config['database'] = "career";
		else	
			$config['database'] = LANGUAGE . "_" . "career";
*/
		$config['dbdriver'] = "mysql";
		$config['dbprefix'] = "";
		$config['pconnect'] = FALSE;
		$config['db_debug'] = TRUE;
		$config['cache_on'] = FALSE;
		$config['cachedir'] = "";
		$config['char_set'] = "utf8";
		$config['dbcollat'] = "utf8_unicode_ci";

		$this->load->database($config);	
//		$this->load->database();
	}

	public function get_skill($args = FALSE)
	{
		$filter = $this->filterStrGet();
		if($filter!=""){
			$filter_json=json_decode($filter,true);
			$value=$filter_json[0]["value"];
			$str = 'SELECT ct.t_photo, ct.t_name, cst.s_id, cst.s_name
				FROM c_teacher as ct, c_skill as cs, c_student as cst 
					INNER JOIN c_student_skill as css 
					ON cst.s_id=css.s_id
				WHERE css.sk_id=cs.sk_id AND cs.sk_name like "%' . $value . '%" AND cs.t_id=ct.t_id';
		} else
			$str = 'SELECT ct.t_photo, ct.t_name, cst.s_id, cst.s_name
                                FROM c_teacher as ct, c_skill as cs, c_student as cst
                                        INNER JOIN c_student_skill as css
                                        ON cst.s_id=css.s_id
                                WHERE css.sk_id=cs.sk_id AND cs.sk_name like "%java%" AND cs.t_id=ct.t_id';
/*
		if ($args === FALSE)
		{
			return NULL;
		}
*/
//		echo "get_news" . $args;
		$query = $this->db->query($str);
//		var_dump($query->row_array());
//		return $query->row_array();
		return $query->result();
	}

	function filterStrGet()
	{
    	  $a = explode('&',$_SERVER["REQUEST_URI"]);
    	  $i = 0;
    	  while ($i < count($a)) {
            $b = explode('=', $a[$i]);
            if(htmlspecialchars(urldecode($b[0]))=="filter")
            {
              $filter=urldecode($b[1]);
              return $filter;
            }
          $i++; 
          }
	}
	
	public function get_student_name($args = FALSE)
	{
		if ($args === FALSE)
		{
			$query = $this->db->get('c_teacher');
			return $query->result_array();
		}
//		echo "get_news" . $args;
		$query = $this->db->get_where('c_student', array('s_id' => $args));
//		var_dump($query->row_array());
		return $query->row_array();
	}

	function listStudents()
	{
		return $this->db->get('c_student');
	}	
	
	function getSkill()
	{
		return $this->db->get('c_teacher_skill');
	}	

	public function set_studentskill()
	{
		$this->load->helper('form');
//		$slug = url_title($this->input->post('t_id'), 'dash', TRUE);
		$skill = $this->input->post('skill');
		$maturity = $this->input->post('maturity');
		$i=0;
		foreach($this->input->post('sid') as $item){
			$data = array(
				's_id' => $item,
				'sk_id' => $skill[$i],
				'maturity' => $maturity[$i]
			);
			$this->db->insert('c_student_skill', $data);
			$i++;
		}


//		return $this->db->insert('c_teacher_skill', $data);
	}
	
	public function set_skill()
	{
//		$this->load->helper('url');
		$this->load->helper('form');
//		$slug = url_title($this->input->post('t_id'), 'dash', TRUE);
		foreach($this->input->post('iptNo') as $item){
			$data = array(
				's_name' => $item,
				't_id' => '090921'
			);
			$this->db->insert('c_teacher_skill', $data);
		}


//		return $this->db->insert('c_teacher_skill', $data);
	}

	
	public function set_news()
	{
		$this->load->helper('url');

//		$slug = url_title($this->input->post('t_id'), 'dash', TRUE);

		$data = array(
			't_id' => $this->input->post('account'),
//			'p_id' => $pid,
			't_name' => $this->input->post('name')
		);

		return $this->db->insert('c_teacher', $data);
	}

}
